package cn.witsky.rtp2ws.config;

import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Configuration
@ConfigurationProperties(prefix = "list")
public class Config {

	private volatile Map<String, String> map = null;
	private List<Record> records = Lists.newArrayList();

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

	
	
	public List<Record> getRecords() {
		return records;
	}

	public void setRecords(List<Record> records) {
		this.records = records;
	}

	public String getHost(String domain) {
		if(map==null) {
			synchronized (this) {
				if(map==null) {
					map=Maps.newHashMap();
					for(Record record:records) {
						map.put(record.getDomain(), record.getHost());
					}
				}
			}
		}
		return map.get(domain);		
	}

	
	public static class Record {
		private String domain;
		private String host;

		public String getDomain() {
			return domain;
		}

		public void setDomain(String domain) {
			this.domain = domain;
		}

		public String getHost() {
			return host;
		}

		public void setHost(String host) {
			this.host = host;
		}

		@Override
		public String toString() {
			return "Record [domain=" + domain + ", host=" + host + "]";
		}
	}
}